<template>
    <div class="my-container">
        <!-- 已经登录 -->
        <div v-if="user" class="header user-info">
            <div class="base-info">
                <div class="left">
                    <van-image class="avatar" fit="cover" round :src="datalist.userInfo.photo" />
                    <span class="name">{{ datalist.userInfo.name }}</span>
                </div>
                <div class="right">
                    <van-button size="mini" to="/profile" round>编辑资料</van-button>
                </div>
            </div>
            <div class="data-stats">
                <div class="data-item">
                    <span class="count">{{ datalist.userInfo.art_count }}</span>
                    <span class="text">头条</span>
                </div>
                <div class="data-item">
                    <span class="count">{{ datalist.userInfo.follow_count }}</span>
                    <span class="text">关注</span>
                </div>
                <div class="data-item">
                    <span class="count">{{ datalist.userInfo.fans_count }}</span>
                    <span class="text">粉丝</span>
                </div>
                <div class="data-item">
                    <span class="count">{{ datalist.userInfo.like_count }}</span>
                    <span class="text">获赞</span>
                </div>
            </div>
        </div>

        <!-- 未登录 -->
        <div v-else class="header not-login">
            <div class="long-btn" @click="router.push('/Login')">
                <img class="mobile-img" src="~@/assets/mobile.png" />
                <span class="text">登录 / 注册</span>
            </div>
        </div>

        <!-- 导航 -->
        <van-grid class="grid-nav" :column-num="2" clickable>
            <van-grid-item class="grid-item">
                <template #icon>
                    <van-icon center class="iconfont headline-shoucang"></van-icon>
                    <span>收藏</span>
                </template>
            </van-grid-item>
            <van-grid-item class="grid-item">
                <template #icon>
                    <van-icon center class="iconfont headline-lishi"></van-icon>
                    <span>历史</span>
                </template>
            </van-grid-item>
        </van-grid>
        <!-- 功能列表 -->
        <van-cell title="消息通知" is-link />
        <van-cell title="小智同学" is-link class="mb-9" />
        <van-cell title="退出登录" v-if="user" @click="onLogout" clickable class="logout-cell" />
        <router-view></router-view>
    </div>
</template>

<script>
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { computed, reactive, onMounted } from 'vue'
import { Dialog, Toast } from 'vant'
import { getUserInfoAPI } from '@/api/user'
export default {
    name: 'My',
    setup() {
        const router = useRouter()
        const datalist = reactive({
            userInfo: {}
        })
        // 获取vuex state中的状态
        const store = useStore()
        const user = computed(() => store.state.user)
        // 退出确认框
        const onLogout = () => {
            Dialog.confirm({
                title: '小白提示：',
                message: '确认退出登录吗？'
            }).then(() => {
                // 清楚本地存储的user已经容器中的user
                store.commit('setUser', null)
            }).catch(() => {
                // on cancel
            })
        }
        // 请求获取用户自己数据
        const getUserInfo = async () => {
            try {
                const { data } = await getUserInfoAPI()
                datalist.userInfo = data.data
            } catch (err) {
                if (err.request.status) {
                    Toast.fail('登录已过期，请重新登录')
                    store.commit('setUser', null)
                }
            }
        }
        onMounted(() => {
            if (user.value?.token) {
                getUserInfo()
            }
        })
        return {
            router,
            user,
            onLogout,
            datalist
        }
    }
}
</script>

<style scoped lang="less">
.van-badge__wrapper {
    display: flex;
    flex-direction: column;
}

.my-container {
    .header {
        height: 361px;
        background: url("~@/assets/banner.png");
        background-size: cover;
    }

    .not-login {
        display: flex;
        justify-content: center;
        align-items: center;

        .long-btn {
            flex-direction: column;
            display: flex;
            justify-content: center;
            align-items: center;

            .mobile-img {
                width: 132px;
                height: 132px;
                margin-bottom: 15px;
            }

            .text {
                font-size: 28px;
                color: #fff;
            }
        }
    }

    .user-info {
        .base-info {
            height: 231px;
            padding: 76px 32px 23px;
            box-sizing: border-box;
            display: flex;
            justify-content: space-between;
            align-items: center;

            .left {
                display: flex;
                align-items: center;

                .avatar {
                    width: 132px;
                    height: 132px;
                    border: 4px solid #fff;
                    margin-right: 23px;
                }

                .name {
                    font-size: 30px;
                    color: #fff;
                }
            }
        }
    }

    .data-stats {
        display: flex;

        .data-item {
            height: 130px;
            flex: 1;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            color: #fff;

            .count {
                font-size: 36px;
            }

            .text {
                font-size: 23px;
            }
        }
    }

    .grid-nav {
        .grid-item {
            height: 141px;

            .iconfont {
                font-size: 45px;
                margin-bottom: 4px;
            }

            .headline-shoucang {
                color: #eb5253;
            }

            .headline-lishi {
                color: #ff9d1d;
            }

            span.text {
                font-size: 28px;
            }
        }
    }

    .logout-cell {
        text-align: center;
        color: #d86262;
    }

    .mb-9 {
        margin-bottom: 9px;
    }
}
</style>
